/**
 * TmPackFactoryServiceImpl.java
 * Created at 2017-3-14
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.dao.ITmPackFactoryDao;
import com.broadtext.rms.basic.model.TmPackFactory;
import com.broadtext.rms.basic.service.ITmPackFactoryService;

/**
 * <p>
 * ClassName: TmPackFactoryServiceImpl
 * </p>
 * <p>
 * Description: 电池pack生产厂服务层实现类
 * </p>
 * <p>
 * Author: xieshuang
 * </p>
 * <p>
 * Date: 2017-3-14
 * </p>
 */
@Service("tmPackFactoryService")
public class TmPackFactoryServiceImpl extends EntityServiceImpl<TmPackFactory> implements ITmPackFactoryService {
    /**
     * 
     * <p>
     * Description: 注入基类dao
     * </p>
     * 
     * @param entityDao entityDao
     */
    @Autowired
    @Qualifier("tmPackFactoryDao")
    public void setEntityDao(EntityDao<TmPackFactory> entityDao) {
        super.entityDao = entityDao;
    }

    /**
     * 注入dao
     */
    @Autowired
    @Qualifier("tmPackFactoryDao")
    private ITmPackFactoryDao tmPackFactoryDao;

    /**
     * 
     * <p>
     * Description: 获取厂区名列表供下拉框使用
     * </p>
     * 
     * @return 结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getPackFactoryNameList() throws DaoException {
        return this.entityDao.findByQuery("com.broadtext.rms.basic.model.TmPackFactory.getPackFactoryNameList", null);
    }

    /**
     * 
     * <p>
     * Description: 根据主键获取对象
     * </p>
     * 
     * @param id 主键
     * @return 对象
     * @throws DaoException 异常
     */
    public TmPackFactory getPackFactoryById(String id) throws DaoException {
        return this.tmPackFactoryDao.getPackFactoryById(id);
    }

    /**
     * 
     * <p>
     * Description: 删除
     * </p>
     * 
     * @param idArray 主键数组
     * @param updateUser 操作人
     * @return 判断标志
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    public String delete(String[] idArray, String updateUser) throws DaoException {
        List<TmPackFactory> list = new ArrayList<TmPackFactory>();
        list = this.entityDao.findByProperty(
                "com.broadtext.rms.basic.model.TmPackFactory.getPackEmployByPackFactoryIds", idArray);
        if (!list.isEmpty()) {
            return "FAILURE";
        }
        for (String id : idArray) {
            TmPackFactory t;
            t = new TmPackFactory();
            t.setPkId(id);
            t.setUpdateDate(new Date());
            t.setUpdateUser(updateUser);
            t.setDelFlag("1");
            this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmPackFactory.updateRecord", t);
        }
        return "success";
    }
    
    /**
     * 
     * <p>Description: 新增or编辑厂区</p>
     * @param t 对象
     * @param currentUser 当前登录人
     * @return 保存标志
     * @throws ServiceException 异常
     * @throws DaoException 异常
     */
    public Object saveOrUpdate(TmPackFactory t, String currentUser) throws ServiceException, DaoException {
        //返回值
        String result = "SUCCESS";
        if (!"".equals(t.getPkId())) {
            //编辑
            if(this.tmPackFactoryDao.isOnly(t)){
                t.setUpdateUser(currentUser);
                t.setUpdateDate(new Date());
                this.updateRecord(t);
            } else {
                result = "DUPLICATE";
            }
            
        } else {
            //新增  验证编码是否唯一
            if(this.tmPackFactoryDao.isOnly(t)){
            	t.setPkId(UUIDUtils.getUUID());
                t.setCreateUser(currentUser);
                t.setCreateDate(new Date());
                t.setUpdateUser(currentUser);
                t.setUpdateDate(new Date());
                t.setDelFlag("0");
                this.insertRecord(t);
            } else {
                //不唯一
                result = "DUPLICATE";
            }
            
        }
        return  result;
    }
}
